var app = getApp();
Page({
  data: {
    allList:[],
    list: [],
    page: 1,       // 请求数据的页码
    displayNum: 1, // 当前显示第几个页面
    lastCur: 0,    // swpier上一个滑动的current
    curPage: 1,    // 当前显示的页面属于第几页，10个一页
    swiperCur: 0,   // swiper的current
    screenHeight: 0,//获取屏幕高度
    statusBarHeight: 0,//获取状态栏高度
    navBarHeight: 0,//获取导航栏高度,
    isPlay:true,
    current:'',//当前视频对象
    length:0,
    pageId:'',
    share_num:0,
    praise_num:0,
    is_praise:0,
    comments_num:0,
    stick_id:'',
    is_follow:1,
    isLoak:false,
    user_id:''
  },
  onShareAppMessage(res) {
    if(res.from == 'button'){
      this.setShareNumber()
    }
    return {
      title:this.data.current.title,
      path: `/packageC/micro_communities/microCommentDetails/microCommentDetails?stickId=${this.data.current.id}`,
      imageUrl: this.data.current.has_one_video.image
    }
  },
  onShow(){
    this.getMicroDetails();
  },
  onReady(){
    this.videoContext = wx.createVideoContext('myVideo')
  },
  onLoad (options) {
    this.setData({
      pageId:options.pageId
    })
    this.getInfo();
    this.getVideoList();
    this.getMicroDetails();
  },
  toDetail(){
    wx.reLaunch({
      url: `/packageC/micro_communities/microCommentDetails/microCommentDetails?stickId=${this.data.current.id}`
    });
    
  },
  goBack(){
    // wx.navigateBack({
    //   delta: 1  // 返回的页面数，如果 delta 大于现有页面数，则返回到首页。
    // });
    wx.reLaunch({
      url: '/packageG/newDiy/newDiy'
    });
  },
  videoPlay: function() {
    let { isPlay } = this.data
    if (isPlay) {
      this.videoContext.pause()
      this.setData({ isPlay: false })
      isPlay = false
    } else {
      this.videoContext.play()
      this.setData({ isPlay: true })
    }
    console.log(this.videoContext, isPlay)
  },
  getInfo(){
    const res = wx.getSystemInfoSync();
    let windowHeight = res.windowHeight;
    let windowWidth = res.windowWidth;
    this.setData({
      screenHeight: res.screenHeight,
      statusBarHeight: res.statusBarHeight+20,
      navBarHeight: res.system.substring(0, 3) == "iOS" ? 42 : 44,
      heightauto: windowHeight + "px",
      widthauto: windowWidth + "px",
    });
  },

  setList (page) {
    let list  = this.data.list
    for(let i=1; i<=10; i++) {
      list.push(i + (page - 1)*10) 
    }
    console.log(list)
    this.setData({
      list: list
    })
  },

//切换视频
  handleChange (event) {
    const current = event.detail.current
    console.log(current)
    // 禁止第一个视频往上滑
    if (current == this.data.length-1 && this.data.lastCur == 0 && this.data.curPage == 1) {
      this.setData({
        swiperCur: 0,
      })
      return
    }

    // if (current == this.data.length-1 && this.data.list[this.data.swiperCur + (this.data.curPage-1)*this.data.length]-1 == this.data.allList.length-1) {
    //   this.setData({
    //     swiperCur: this.data.length-1,
    //   })
    //   return
    // }

    if (current + (this.data.curPage-1)*this.data.length + 2 == this.data.list.length) {
      this.data.page ++
      this.setList(this.data.page)
    }

    if (current > this.data.lastCur) {
      if (current == this.data.length-1 && this.data.lastCur == 0) {
        this.data.displayNum --
      }else {
        this.data.displayNum ++
      }
    }else {
      if (current == 0 && this.data.lastCur == this.data.length-1) {
        this.data.displayNum ++
      }else {
        this.data.displayNum --
      }
    }

    if (this.data.curPage == 0 || this.data.displayNum == 0) {
      this.data.curPage = 1
      this.data.displayNum = 1
    }

    this.setData({
      curPage: Math.ceil(this.data.displayNum / this.data.length),
      swiperCur: current
    })

    this.data.lastCur = current;
    let index = (this.data.list[this.data.swiperCur + (this.data.curPage-1)*this.data.length]-1)%this.data.length;
    this.setData({
      current:this.data.allList[index],
      pageId:this.data.allList[index].id
    })

    this.getMicroDetails();

  },
  //点赞
  handleClickPraise(){
    let that = this
    if(this.data.isLoak){
      return;
    }
    this.setData({
      isLoak:true
    })
    this.setData({
      is_praise:this.data.is_praise == 0 ? 1 : 0,
      praise_num:this.data.is_praise == 0 ? this.data.praise_num*1+1 : this.data.praise_num*1-1
    })
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.trick.givePraise");;
    let dataJson = {};
    dataJson.tid = this.data.stick_id;
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: function (resdata) {
        var res = resdata.data;
        that.setData({
          isLoak:false
        })
        if (res.result == 1) {
          //that.getMicroDetails();
        }else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
       
      },fail: function(res) {
        that.setData({
          isLoak:false
        })
      }
    })  
  },
  //取消点赞
  handleClickPraiseCancel(){
    let that = this
    if(this.data.isLoak){
      return;
    }
    this.setData({
      isLoak:true
    })
    this.setData({
      is_praise:this.data.is_praise == 0 ? 1 : 0,
      praise_num:this.data.is_praise == 0 ? this.data.praise_num*1+1 : this.data.praise_num*1-1
    })
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.trick.cancel-praise");;
    let dataJson = {};
    dataJson.tid = this.data.stick_id;
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: function (resdata) {
        var res = resdata.data;
        that.setData({
          isLoak:false
        })
        if (res.result == 1) {
          //that.getMicroDetails();
        }else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
       
      },fail: function(res) {
        that.setData({
          isLoak:false
        })
      }
    })  
  },
  //帖子详情
  getMicroDetails() {
    let that = this;
    let dataJson = {
      tid: this.data.pageId
    };
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.trick.getStrickDetail");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success:  (resdata)=> {
        var res = resdata.data;
        that.setData({
          info:res.data,
          share_num:res.data.share_num,
          praise_num:res.data.praise_num,
          is_praise:res.data.is_praise,
          stick_id:res.data.has_one_video.stick_id,
          is_follow:res.data.is_follow,
          user_id:res.data.user_id,
          comments_num:res.data.comments_num
        })
      },
      fail: function (res) {}
    });
  },
  //分享次数
  setShareNumber(){
    let that = this;
    let dataJson = {
      tid: this.data.stick_id
    };
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.trick.share");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success:  (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          that.getMicroDetails()
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {}
    });
  },
  //关注
  handleFollow(){
    let that = this
    if(this.data.isLoak){
      return;
    }
    this.setData({
      isLoak:true
    })
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.user.addFollow");;
    let dataJson = {};
    console.log(urlStr)
    dataJson.user_id = this.data.user_id;
    this.setData({
      is_follow:1
    })
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: function (resdata) {
        var res = resdata.data;
        that.setData({
          isLoak:false
        })
        if (res.result == 1) {
          //that.getMicroDetails();
          wx.showToast({
            icon: 'none',
            title: '关注成功',
            duration: 1500
          });
        }else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
       
      },fail: function(res) {
        that.setData({
          isLoak:false
        })
      }
    }) 
  },
  //视频列表
  getVideoList(){
    let that = this
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.index.getStick&resource_type=2");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if(res.data.data.length < 10){
            that.setData({
              length:res.data.data.length
            })
          }else{
            that.setData({
              length:10
            })
          }
          that.setList(that.data.page);
          let first = res.data.data.find(item=>{
            return item.id == that.data.pageId
          })
          that.setData({
            current:first,
            allList:res.data.data
          })
        } 
      },
      fail: function(res) {}
    });
  }
})
